﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.SharePoint.WebControls;
using Microsoft.SharePoint;
using System.Data;
using System.Collections;
using LookupFieldEntityPicker.Helpers;

namespace LookupFieldEntityPicker.CustomLookup
{
    public class RecordPickerQueryControl : SimpleQueryControl
    {
        private RecordPickerDialog RecordPickerDialog
        {
            get
            {
                return (RecordPickerDialog)base.PickerDialog;
            }
        }

        public RecordPickerQueryControl()
        {
            Load += new EventHandler(RecordPickerQueryControl_Load);
        }

        void RecordPickerQueryControl_Load(object sender, EventArgs e)
        {
            //get from info field wthich is custom field
            string fields = RecordPickerDialog.CustomProperty;
            if (!Page.IsPostBack)
            {
                EnsureChildControls();

                if (!String.IsNullOrEmpty(fields))
                {
                    SPList list = RecordPickerDialog.List;
                    if (list != null)
                    {
                        string[] groups = fields.Split(new char[] { ';' }, StringSplitOptions.RemoveEmptyEntries);
                        foreach (string group in groups)
                        {
                            if (list.Fields.ContainsField(group))
                            {
                                SPField field = list.Fields.GetFieldByInternalName(group);
                                mColumnList.Items.Add(new System.Web.UI.WebControls.ListItem(field.Title, group));
                            }
                        }
                    }
                }
            }
        }

        protected override void OnLoad(EventArgs e)
        {
            EnsureChildControls();
            
            base.OnLoad(e);
        }


        public override PickerEntity GetEntity(DataRow dr)
        {
            var entity = new PickerEntity
            {
                Key = (string)dr["ID"],
                IsResolved = true,
                DisplayText = (string)dr[Constants.DEPARTMENT_CODE],
                Description = (string)dr[Constants.DEPARTMENT_CODE] + ":" + (string)dr[Constants.DEPARTMENT_NAME]
            };

            entity.EntityData.Add((string)dr["ID"], (string)dr[Constants.DEPARTMENT_CODE]);
            return entity;
        }


        protected override int IssueQuery(string search, string groupName, int pageIndex, int pageSize)
        {
            DataTable resultsTable = GetDataTable(search, groupName);
          
            RecordPickerDialog.Results = resultsTable;
            return resultsTable.Rows.Count;
        }

        private DataTable GetDataTable(string search, string groupName)
        {
            DataTable table = new DataTable();
            table.Columns.Add("ID");
            table.Columns.Add(Constants.DEPARTMENT_CODE);
            table.Columns.Add(Constants.DEPARTMENT_NAME);
            table.Columns.Add(Constants.DEPARTMENT_DESCRIPTION);
           

            List<DepartmentEntity> departments;
            switch (groupName)
            {
                        
                case Constants.DEPARTMENT_CODE: 
                    departments = ListHelpers.FindDepartments(Constants.DEPARTMENT_CODE, search);
                    break;
                case Constants.DEPARTMENT_NAME:
                    departments =  ListHelpers.FindDepartments(Constants.DEPARTMENT_NAME, search);
                    break;
                default:
                    departments = ListHelpers.FindDepartments(null, null);
                    break;
            }

            foreach (DepartmentEntity department in departments)
            {
                var row = table.NewRow();
                row["ID"] = department.ID;
                row[Constants.DEPARTMENT_NAME] = department.Name;
                row[Constants.DEPARTMENT_CODE] = department.Code;
                row[Constants.DEPARTMENT_DESCRIPTION] = department.Description;
               
                table.Rows.Add(row);
            }
           
            return table;
        }
    }
}
